package com.c.ctgproject.ui.collect

import android.os.Handler
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.OutCommonAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryOutWarehouseBinding
import com.c.ctgproject.entity.guang.OutCommonEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述: 动力电池出库单 页面 电池出库单 页面
 */
class PowerBatteryOutWarehouseActivity : BaseActivityV2(),OutCommonAdapter.OutCommonCallBack {
    private val mBinding by lazy { ActivityBatteryOutWarehouseBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var search = ""
    private var page = 1
    private val mList = ArrayList<OutCommonEntity.DataBean.ListBean>()
    private val mAdapter by lazy { OutCommonAdapter(this, mList, 1,this) }

    override fun event() {
        getData()
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
    }

    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
                "销售出库" -> {
                    outType = 1
                    page = 1
                    getData()
                }
                "借用出库" -> {
                    outType = 2
                    page = 1
                    getData()
                }
                "其他出库" -> {
                    outType = 3
                    page = 1
                    getData()
                }
            }
        }


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    //出库类型 1-销售出库 2-借用出库 3-其他出库
    private var outType = 1
    private fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.commonOutCheck(1, search, page, 1, outType), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("出库单列表回调:$it")
            val data = getGson().fromJson(it, OutCommonEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerView.handlerPosition(page)
            }
        })

//        ApiMethods.apiSubscribe(ApiMethods.retrofit.commonOut(search, page, outType), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
//            MyLog.d("动力电池出库单查询回调:$it")
//            val data = getGson().fromJson(it, PowerBatteryOutEntity::class.java).data
//            data?.list?.let { list ->
//                if (page == 1) mList.clear()
//                mList.addAll(list)
//                mAdapter.notifyDataSetChanged()
//            }
//        })
    }

    override fun itemDelete(position: Int, data: OutCommonEntity.DataBean.ListBean) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.deletePartsDismantle(data.outOrderId?:0), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("删除结果:$it")
            val data = getGson().fromJson(it, OutCommonEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mRecyclerView.handlerPosition(page)
            }
        })
    }


}